<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <router-link to="">
          <div class="title-bar">
            <img src="../assets/logo.png" alt="logo" class="logo">
            <div class="title">
              YM网盘
            </div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu v-model:selectedKeys="current" mode="horizontal" :items="items" @click="doMenuClick" />
      </a-col>
      <a-col  flex="120px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            {{loginUserStore.loginUser.avatar }}
            {{loginUserStore.loginUser.userName ?? '无名'}}
          </div>
          <div v-else>
            <a-button type="primary" @click="showLoginModal">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>

  </div>
</template>

<script lang="ts" setup>
import { h, ref } from 'vue';
import { HomeOutlined, AppstoreOutlined, SettingOutlined } from '@ant-design/icons-vue';
import { MenuProps } from 'ant-design-vue';
import { useRouter } from 'vue-router'
import { useLoginUserStore } from "@/stores/useLoginUseStore.ts"


// 控制登录弹窗显示
const loginVisible = ref(false);

// 显示登录弹窗
const showLoginModal = () => {
  // 直接跳转到登录页面
  router.push('/login');
};



const loginUserStore = useLoginUserStore()

const items = ref<MenuProps['items']>([
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/admin',
    icon: () => h(AppstoreOutlined),
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/about',
    icon: () => h(AppstoreOutlined),
    label: '关于',
    title: '关于',
  },
  {
    key: '/other',
    icon: () => h(SettingOutlined),
    label: h('a', { href: 'https://antdv.com', target: '_blank' }, '其他'),
    title: '其他',
  },
]);

const router = useRouter();

//路由跳转
const doMenuClick  = ({ key }) => {
  router.push({
    path: key
  })
}


const current = ref<string[]>(['mail']);
router.afterEach((to, from, next ) =>{
  current.value = [to.path];
})

</script>

<style scoped>
#globalHeader .title-bar{
  display: flex;
  align-items: center;
}
#globalHeader .logo{
  height: 48px;
}
#globalHeader .title{
  color: black;
  font-size: 24px;
  margin-left: 16px;
}
</style>

